package com.lianxin.fmank_backstage.dao;

import com.lianxin.fmank_backstage.pojo.FlowOverView;
import org.springframework.stereotype.Repository;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

@Repository
public class TrafficAnalyDaoImpl implements TrafficAnalyDao {
    Connection connection;
    @Override
    public List<FlowOverView> getTrafficAnalyRecords(String sql) {
        ArrayList<FlowOverView> list = new ArrayList<>();

        String url = "jdbc:presto://linux01:8080/hive/yiee_pro_db";
        Properties pros = new Properties();
        pros.setProperty("user","root");
        pros.setProperty("SSL","false");
        try {
            connection = DriverManager.getConnection(url, pros);
            Statement statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery(sql);
            while (resultSet.next()){
                String dt = resultSet.getString("dt");
                int pv_cnts = resultSet.getInt("pv_amt");
                int uv_cnts = resultSet.getInt("uv_amt");
                int se_cnts = resultSet.getInt("session_amt");
                double av_se_time = resultSet.getDouble("avg_time_s");
                double av_u_se = resultSet.getDouble("avg_sessions_u");
                double av_u_pv = resultSet.getDouble("avg_pvcnts_u");
                double av_u_time = resultSet.getDouble("avg_time_u");
                double reb_u_ratio = resultSet.getDouble("reback_u_ratio");
                FlowOverView flowOverView = FlowOverView.of(dt, pv_cnts, uv_cnts, se_cnts, av_se_time, av_u_se, av_u_pv, av_u_time, reb_u_ratio);
                list.add(flowOverView);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }

       /* FlowOverView flowOverView = FlowOverView.of("2019-11-27", 10, 6, 2, 11.22, 22.22, 33.11, 1.2, 0.1);
        list.add(flowOverView);*/


        return list;
    }
}
